<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Delpiero22
 * Date: 1/3/2555
 * Time: 23:30 น.
 * To change this template use File | Settings | File Templates.
 */
 
class stat extends CI_Controller {


    function index()
    {
        print ("please select room or type of graph or more of duration");
    }
    function graph()
    {
        //get post from chart_view
        $post = $this->input->post();
        $room = isset($post['room']) == true ? $post['room'] : 1202;
        $month = isset($post['month']) == true ? $post['month'] : 'jan';
        //$view = isset($post['view']) == true ? $post['view'] : 'month';
        //$year = isset($post['year']) == true ? $post['year'] : 2012;
        //$type = isset($post['type']) == true ? $post['type'] : "bar";
        //$parameter = isset($post['parameter']) == true ? $post['parameter'] : "2012-03-%";

        //print_r($post);

        //case of month
    /*    switch($view){
            case "day":
                $this->day($room,$view);//call day function
                break;
            case "month":
                $this->month($room,$view);//call month function
                break;
            case "year":
                $this->year($room,$view);//call year function
                break;
        }*/
        if($month == "jan"OR"mar"OR"may"OR"july"OR"aug"OR"oct"OR"dec"){
            $this->month_31($room,$month);//call month_31 function
        }
        else if($month == "april"OR"june"OR"sep"OR"nov"){
            $this->month_30($room,$month);//call month_30 function

        }
        else{
            $this->february($room,$month);//call february function
        }




    }
    /*private function plotGraph ($room,$view){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$view));

        //set data in array
        $datay = array();
        $datax = array();


                for($i=1;$i<32;$i++){
                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->barChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $view;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }*/

    private function month_31 ($room,$month){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$month));

        //set data in array
        $datay = array();
        $datax = array();

                      
                for($i=1;$i<32;$i++){
                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->barChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $month;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }
    private function month_30 ($room,$month){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$month));

        //set data in array
        $datay = array();
        $datax = array();


                for($i=1;$i<31;$i++){
                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->barChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $month;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }
    private function february ($room,$month){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$month));

        //set data in array
        $datay = array();
        $datax = array();


                for($i=1;$i<30;$i++){
                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->barChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $month;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }

   /* private function year ($room,$view){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$view));

        //set data in array
        $datay = array();
        $datax = array();


            for($i=1;$i<31;$i++){
                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->barChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $view;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }*/


    /*private function Pie($room,$parameter){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$parameter));

        //set data in array
        $datay = array();
        $datax = array();


            for($i=1;$i<31;$i++){


                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->pieChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $parameter;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }

    private function Line($room,$parameter){
        // load library and function need
        $this->load->library('jpgraph');
        $this->load->helper('url');//for use base 64
        $this->load->database();

        //action with database (query)
        $sql = "SELECT COUNT(`resid`) as count,`usedate`
                FROM `reservation`
                WHERE `roomid` = ? AND `usedate` LIKE ? GROUP BY `usedate`";
        $query = $this->db->Query($sql,array($room,$parameter));

        //set data in array
        $datay = array();
        $datax = array();


            for($i=1;$i<31;$i++){


                $isValid = false;
                foreach($query->result_array() as $value){
                    $date =  preg_split('/[-]/', $value['usedate']);//split "-" string of usedate
                    if($date[2] == $i){
                        $datax[] = $i;
                        $datay[] = $value['count'];
                        $isValid = true;
                        break;
                    }
                }

                if(!$isValid){
                    $datax[] = "$i";
                    $datay[] = 0;
                }
            }


        //add data from database in grahph variable with parameter to send to jpgraph library
        $graph = $this->jpgraph->lineChart($datay,$datax,"Room"."$room");

        // File
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';   //the name of file graph is test.png
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file

        $data['input'] = $parameter;
        $data['graph'] = base64_encode(file_get_contents($graph_file_location));
        @unlink($graph_file_location);//if file has in directory , delete old file

        $this->load->view("chart_view",$data);//send data variable to View
    }*/
}
?> 